﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Net;
using System.Windows.Forms;
using Portal.GUI;
using Portal.Helpers;
using Portal.Core;
using Portal.Managers;
using Styx;
using Styx.Common;
using Styx.CommonBot;
using Styx.CommonBot.Routines;
using Styx.TreeSharp;
using Styx.WoWInternals.WoWObjects;

namespace Portal
{
    public partial class Portal : CombatRoutine
    {
        public override sealed string Name { get { return "Portal Free [Millz]"; } }
        public override WoWClass Class { get { return WoWClass.Mage; } }
        private static LocalPlayer Me { get { return StyxWoW.Me; } }
        private static Portal Instance { get; set; }
        private Form _PortalGui;
        internal event OnPulseHandler PulseEvent;
        internal delegate void OnPulseHandler();

        public override void Initialize()
        {
            try
            {

            CultureInfo customCulture = (CultureInfo)System.Threading.Thread.CurrentThread.CurrentCulture.Clone();
            customCulture.NumberFormat.NumberDecimalSeparator = ".";
            System.Threading.Thread.CurrentThread.CurrentCulture = customCulture;

            TalentManager.Update(); // Update talents
            
            RoutineManager.Reloaded += (s, e) =>
            {
                Log.Debug("Creating Behaviors");
                RebuildBehaviors();
            };

            // Init Behaviours
            if (_combatBehavior == null)
                _combatBehavior = new PrioritySelector();

            if (_preCombatBuffBehavior == null)
                _preCombatBuffBehavior = new PrioritySelector();

           
            if (!RebuildBehaviors())
            {
                return;
            }

            Log.Debug("Behaviors Created");

            // PRINT OUT CLIENT LATENCY
            Log.Debug("Client Latency: {0}", StyxWoW.WoWClient.Latency);

            // PRINT OUT LAST RESTART TIME
            Log.Debug("{0:F1} days since Windows was started.", TimeSpan.FromMilliseconds(Environment.TickCount).TotalHours / 24.0);

            // PRINT OUT SETTINGS
            Settings.PortalSettings.LogSettings();

            Log.Info("Thanks for choosing Portal Free! [Update: 26-Aug-2013]");
            Log.Info("You are a level {0} {1} ({2})", Me.Level, Me.Race, Me.IsAlliance ? "For the Alliance!" : "For the Horde!");
            if (Me.IsInInstance) { Log.Info("You are in an instance"); }
            if (Me.CurrentMap.IsArena) { Log.Info("You are in arena: {0}", Me.CurrentMap.Name); }
            if (Me.CurrentMap.IsDungeon) { Log.Info("You are in dungeon: {0}", Me.CurrentMap.Name); }
            if (Me.CurrentMap.IsBattleground) { Log.Info("You are in battleground: {0}", Me.CurrentMap.Name); }
            if (Me.CurrentMap.IsContinent) { Log.Info("You are in zone: {0}", Me.CurrentMap.Name); }
            }
            catch (Exception ex)
            {
                Log.Debug("Exception thrown at Initialize: {0}", ex.ToString());
            }
        }


        public Portal()
        {
            Instance = this;
        }

        public override void Pulse()
        {
            try
            {
                Spell.PulseDoubleCastEntries();
                OnPulseHandler handler = PulseEvent;

                if (handler != null)
                    handler();

            }
            catch (Exception ex)
            {
                
                Log.Debug("Exception in Pulse: {0}", ex);
            }
            
        }


        #region Extensions/Utilities

        private static void StopBot(string reason)
        {
            if (TreeRoot.IsRunning)
            {
                Log.Debug(reason);
                TreeRoot.Stop();
            }
                
        }

        public override bool WantButton
        {
            get { return true; }
        }


        public override void OnButtonPress()
        {
            if (_PortalGui == null || _PortalGui.IsDisposed || _PortalGui.Disposing) _PortalGui = new ConfigurationForm();
            if ((_PortalGui != null || _PortalGui.IsDisposed)) _PortalGui.ShowDialog();
        }


        #endregion

    }
}
